<?php

class Model_Top5 extends Model {

    function __construct() {
        $this->Lookup = getModel('Model_Lookup');
    }

    function getList() {
        $nums = 5;
        $top_product_num = 0;
        $topids = "";
        $new_product_num = 0;
        $products_top_array = array();
        $products_new_array = array();
        $products_array = array();
        $products_ids = "";
        $result = array();

        $param = array();
        $param['field'] = "TB2.id";
        $param['where'] = "TB1.status=1 AND TB2.status=1 and TB1.is_accept=1 AND TB1.addtime >= date_sub(now(),INTERVAL 2 WEEK)";
        $param['order'] = "TB1.id desc";
        $param['group'] = "TB1.oid";
        $param['limit'] = 99;
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "left join product";
        $param['on'] = "TB1.oid = TB2.id";
        $data = db_query_hash('product_reports', $param);
        if ($data['result']) {
            $top_product_num = count($data['result']);
            $products_top_array = $data['result'];
            foreach ($data['result'] as $value) {
                $topids .= $value['id'] . ",";
            }
            $topids = substr($topids, 0, -1);
        }
        if ($top_product_num < $nums) {
            $new_product_num = $nums - $top_product_num;
            $param = array();
            $where = "report_endtime <= now() and status=1 ";
            if ($topids)
                $where.=" and id not in(" . $topids . ")";
            $param['field'] = "id";
            $param['where'] = $where;
            $param['order'] = "report_endtime desc";
            $param['group'] = "";
            $param['limit'] = $new_product_num;
            $param['pagesize'] = "";
            $param['page'] = "";
            $param['join'] = "";
            $param['on'] = "";
            $data_NewProduct = db_query_hash('product', $param);
            if ($data_NewProduct['result'])
                $products_new_array = $data_NewProduct['result'];
        }
        $products_array = array_merge($products_top_array, $products_new_array);
        if ($products_array) {
            foreach ($products_array as $value) {
                $products_ids .= $value['id'] . ",";
            }
            $products_ids = substr($products_ids, 0, -1);
        }

        if ($products_ids) {
            $param = array();
            $where = "TB1.id in(" . $products_ids . ") and TB1.status=1";
            $param['field'] = "TB1.id,TB1.tag,TB1.attribute_value1,TB1.apply_endtime,TB1.reports,TB1.name,TB1.cname,TB1.brand,TB1.product_type,TB1.quota,(TB1.apply_num + TB1.apply_num_offset) as apply_num ,(TB1.click + TB1.click_offset) as click,(TB1.likes + TB1.likes_offset) as likes,TB1.body,TB1.about_url,TB1.cover_image1 as cover,TB1.is_hightlight,TB1.member_type,TB1.apply_starttime,TB1.report_starttime,TB1.report_endtime,TB1.price_trial,TB1.price,TB1.capacity_trial,TB1.product_layout_category,(TB2.scores+TB2.scores_base) as scores,TB1.is_trial";
            $param['where'] = $where;
            $param['order'] = "scores desc";
            $param['group'] = "";
            $param['limit'] = "5";
            $param['pagesize'] = "";
            $param['page'] = "";
            $param['join'] = "left join product_reports_stat";
            $param['on'] = "TB1.id = TB2.oid";
            $data_NewProduct = db_query_hash('product', $param);
            if ($data_NewProduct['result']) {
                $brandarr = array();
                $brand = $this->Lookup->getLookup('brand');
                if ($brand['result']) {
                    foreach ($brand['result'] as $value) {
                        $brandarr[$value['id']] = $value['name'];
                    }
                }

                $res = array();
                foreach ($data_NewProduct['result'] as $key => $value) {
                    $value['type_name'] = @$brandarr[$value['brand']];
                    $res[$key] = $value;
                }
                $result = $res;
            }
        }


        return $result;
    }

}

?>